#!/bin/bash
# collect network status [ping/trace], and upload to ftpserver

LOCALIP=$1
TARGETIP=$2
TMPFOLDER=/tmp
FTPSERVERURL=ftp://hntest:forhntest@ftp.cloudwww-demo1.com

CMD_PING=`which ping`
CMD_TRACE=`which tracert`
CMD_LFTP=`which lftp`

TIMESTAMP=`date +%Y%m%d%H%M%S`
# FILENAME= LOCALIP__TARGETIP__METHOD__TIMESTAMP.txt

LIP=`echo $LOCALIP | tr "." "_" `
TIP=`echo $TARGETIP | tr "." "_" `

PING_FILENAME="${LIP}__${TIP}__PING__${TIMESTAMP}.txt"
TRACE_FILENAME="${LIP}__${TIP}__TRACE__${TIMESTAMP}.txt"

usage(){
	"$0 LOCALIP TARGETIP"
	"The ping and trace result would upload to a ftp server"
	exit 1
}

errmsg(){
	echo $1
	exit 1
}

debugmsg(){
	echo $1
}

# check environment
for CMD in "$CMD_PING" "$CMD_TRACE" "$CMD_LFTP"
do
	test -f $CMD  || errmsg "$CMD not exist"
done

# check input
if [ "$#" != "2" ]; then
	usage
fi

$CMD_PING -c 4 $LOCALIP || errmsg "$LOCALIP unpingable"
$CMD_PING -c 4 $TARGETIP || errmsg "$TARGETIP unpingable"

# collect testing data
$CMD_PING -c 100 $TARGETIP > $TMPFOLDER/$PING_FILENAME
$CMD_TRACE -n $TARGETIP > $TMPFOLDER/$TRACE_FILENAME

# send testing data
$CMD_LFTP $FTPSERVERURL -e "put $TMPFOLDER/$TRACE_FILENAME;quit"
$CMD_LFTP $FTPSERVERURL -e "put $TMPFOLDER/$PING_FILENAME;quit"

# clear file
rm -f $TMPFOLDER/$PING_FILENAME
rm -f $TMPFOLDER/$TRACE_FILENAME
